package cn.com.pc.content.service;

import cn.com.pc.content.constant.TagConstants;
import cn.com.pc.content.domain.Biz;
import cn.com.pc.content.domain.Site;
import cn.com.pc.content.domain.tag.TagRepository;
import cn.com.pc.content.domain.tag.TagTree;
import cn.com.pc.content.domain.tag.TagTreeRepository;
import cn.com.pc.content.dto.PageForm;
import cn.com.pc.content.dto.PageResult;
import cn.com.pc.content.dto.Sort;
import cn.com.pc.content.dto.TagTreeDTO;
import cn.com.pc.content.service.es.ESCurdService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.transaction.Transactional;
import java.util.Map;

@Slf4j
@Service
public class TagTreeService {

    @Autowired
    private TagTreeRepository tagTreeRepository;

    @Autowired
    private TagRepository tagRepository;

    @Autowired
    private ESCurdService esCurdService;

    /**
     * 新增标签导航树
     * @param tagTreeDTO
     * @return
     */
    public Long createTagTree(TagTreeDTO tagTreeDTO) {
        TagTree tagTree = new TagTree();
        BeanUtils.copyProperties(tagTreeDTO, tagTree);
        tagTree = tagTreeRepository.save(tagTree);
        return tagTree.getId();
    }

    public TagTree getTagTreeById(Long id) {
        return tagTreeRepository.findById(id).orElse(null);
    }

    public boolean existsTagTreeByName(Site site, Biz biz, String name) {
        return tagTreeRepository.existsBySiteAndBizAndName(site, biz, name);
    }


    /**
     * 查询标签导航树列表
     * @param tagTreeDTO
     * @param pageForm
     * @param sort
     * @return
     */
    public PageResult<TagTree> listTagTreesByDTO(TagTreeDTO tagTreeDTO, PageForm pageForm, Sort sort) {
        //查询对象
        TagTree tagTree = new TagTree();
        tagTree.setSite(tagTreeDTO.getSite());

        if (tagTreeDTO.getBiz() != null) {
            tagTree.setBiz(tagTreeDTO.getBiz());
        }
        if (tagTreeDTO.getId() != null) {
            tagTree.setId(tagTreeDTO.getId());
        }
        if (StringUtils.hasLength(tagTreeDTO.getName())) {
            tagTree.setName(tagTreeDTO.getName());
        }
        if (tagTreeDTO.getParentId() != null) {
            tagTree.setParentId(tagTreeDTO.getParentId());
        }

        Example<TagTree> example = Example.of(tagTree);
        Pageable pageable;
        if (!StringUtils.hasLength(sort.getSortBy())) {
            sort.setSortBy("id");
        }
        pageable = PageRequest.of(pageForm.getPageNum() - 1, pageForm.getPageSize(), sort.getDirection(), sort.getSortBy());

        Page<TagTree> tagTreePage = tagTreeRepository.findAll(example, pageable);

        return PageResult.fromPage(tagTreePage);
    }


    /**
     * 修改标签导航树
     * @param tagTreeDTO
     * @param tagTree
     * @return
     */
    public TagTree updateTagTreeByDTO(TagTreeDTO tagTreeDTO, TagTree tagTree) {
        BeanUtils.copyProperties(tagTreeDTO, tagTree, "id", "createdAt");
        return tagTreeRepository.save(tagTree);
    }


    /**
     * 删除标签树
     * @param tagTreeId
     */
    @Transactional
    public void deleteTagTree(Long tagTreeId) {
        // 关联标签置0
        int count = tagRepository.updateTreeId(tagTreeId);

        tagTreeRepository.deleteById(tagTreeId);

        esCurdService.updateByQuery(TagConstants.TAG_IDX, QueryBuilders.termQuery("treeId", tagTreeId), Map.of("treeId", 0) );
    }

}
